<template>
   <PanelSplitTwo bottomToolbarHeight="0" :num="2">
    <template #top>
      <SetUserSelect
        class="queryForm"
        v-model:formData="orderSelect"
        v-model:queryParams="paginationParams"
        @selectFunc="selectFunc"
        :labelwidth="150"
      ></SetUserSelect>
    </template>
     <template #centerToolbar>
        <div class="vex_flex">
          <el-row :gutter="20" style="margin-bottom: 10px">
            <el-col :span="1.5">
              <el-button @click="handleQuery(0)">新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button :disabled="selectedList.length !=1" @click="handleQuery(1)">修改</el-button>
            </el-col>
            <!-- <el-col :span="1.5">
              <el-button :disabled="selectedList.length !=1" @click="publishFun">发布</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button :disabled="selectedList.length !=1" @click="notPublishedFun">下架</el-button>
            </el-col> -->
            <!-- <el-col :span="1.5">
              <el-button @click="handleQuery">取消</el-button>
            </el-col> -->
          </el-row>
        <VxeTableGrid
          :key="keys"
          style="flex: 1;"
          :tableColumn="tableColumn"
          :tableLoading="tableLoading"
          :dataList="dataList"
          :showPagination="true"
          :paginationParams="paginationParams"
          @getList="setQueryParams"
          @selectedChange="selectedChange"
        >
        </VxeTableGrid>
      </div>
     </template>
   </PanelSplitTwo>
</template>

<script setup lang="jsx">
import { useRouter } from 'vue-router'
import { trainingList, publish, notPublished  } from "@/api/trainingManagement";
import { getCurrentInstance, ref } from "vue";
import datas from './data.js'

const { paginationParams, orderSelect }  = datas
const { proxy } = getCurrentInstance();
let proxys = proxy;
const router= useRouter ()
const dialogVisible = ref(false)
const handleClose = () => {
  dialogVisible.value = false
}
const { training_manage_type } = proxy.useDict("training_manage_type");
let keys = ref(0);

const dataLists = ref([])
const tableColumn = ref([
  { type: "checkbox", width: 60, fixed: null },
  { field: "name", title: "培训名称" },
  { field: "drugName", title: "品种" },
  { field: "trainingTime", title: "培训召开时间"},
  { field: "publishedTime", title: "发布时间"},
  { field: "status", title: "状态",
  slots: { default: ({ row }, h) => {
    return [<dict-tag options={ training_manage_type.value } value={ row.status }></dict-tag>]
  }} },
])
const onlineList = ref([]);
const total = ref(0);
const pageNum = ref(1);
const pageSize = ref(10);
let tableLoading = ref(false)
const dataList = ref([
])

const queryParams = ref({
  ipaddr: undefined,
  userName: undefined
});
// 切换分页
  function setQueryParams(val) {
    console.log(val)
    paginationParams.pageNum = val.pageNum;
    paginationParams.pageSize = val.pageSize;
    hospitalLists()
  }
  let Id = ref('')
  let status = ref(null)
  // 选择表格
  let selectedList = ref([])
  function selectedChange(val) {
    Id.value = val.currentItem.id
    selectedList.value = val.selectedList
    status.value = val.currentItem.status
  }
/** 新增|修改 */
function handleQuery(type) {
  router.push({
    path: '/trainingManagement/addTraining',
    query: {
      id: type == 1? Id.value : undefined,
      type: type == 1? undefined : 'add'
    }
  })
}

const hospitalLists = () => {
  // platformNum
  trainingList({
    ...paginationParams.value,
  }).then((res) => {
    paginationParams.value.total = res.total
    dataList.value = res.rows
  })
}
hospitalLists()
const selectFunc = () => {
  paginationParams.value.pageNum = 1
  hospitalLists()
}


const publishFun = () => {
  if (status.value == 1 || status.value == 4) {
    publish({
      id: Id.value
    }).then((res) => {
      proxys.$modal.msgSuccess(`发布成功`);
      hospitalLists()
      keys.value = new Date().getTime()
      selectedList.value = []
    })
  } else {
    proxys.$modal.msgWarning('未发布跟下架的任务才能发布')
  }
  
}

const notPublishedFun = () => {
  if (status.value == 2) {
    notPublished({
      id: Id.value
    }).then((res) => {
      proxys.$modal.msgSuccess(`下架成功`);
      hospitalLists()
      keys.value = new Date().getTime()
      selectedList.value = []
    })
  } else {
    proxys.$modal.msgWarning('已发布的任务才能下架')
  }
  
}
</script>
<style scoped>
  .app-container {
    display: flex;
    flex-direction: column;
    height: calc(100vh - 85px);
    overflow: hidden;
  }
  .vex_flex {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    height: 100%;
  }
  .flexmin {
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }
</style>